package com.hyt.andan.KillXp;

import io.github.bedwarsrel.BedwarsRel.Game.Game;
import ldcr.BedwarsXP.Utils.SoundMachine;
import ldcr.BedwarsXP.XPManager;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public class KillXp extends JavaPlugin implements Listener{
    @Override
    public void onEnable(){
        getServer().getConsoleSender().sendMessage("§b§l[KillXp]§a§l插件加载成功,作者QQ847091302!");
        ConfigLoad();
        Bukkit.getPluginManager().registerEvents(this, this);
    }
    public  void ConfigLoad(){
        File f = new File(getDataFolder() + "/config.yml");
        if (f.exists())
        {
            getServer().getConsoleSender().sendMessage("§b§l[KillXp]§a§l检测到config.yml,开始加载配置!");
        }
        else
        {
            getServer().getConsoleSender().sendMessage("§b§l[KillXp]§c§l未检测到config.yml,正在创建默认配置文件!");
            saveDefaultConfig();
        }
        reloadConfig();


    }
    @EventHandler
    public void onDeathEvent(PlayerDeathEvent e){

        Game bw = io.github.bedwarsrel.BedwarsRel.Main.getInstance().getGameManager().getGameOfPlayer(e.getEntity());
        if (bw == null) {
            return;
        }
        Entity entity = e.getEntity();
        if(e.getEntity().getKiller() != null) {
            Player p = e.getEntity().getPlayer();
            Player k = e.getEntity().getKiller();
            String pname = p.getName();
            String kname = k.getName();
            int bili = getConfig().getInt("bili");
            int golbal = getConfig().getInt("golbal");
            int count = (XPManager.getXP(bw.getName(), p)*bili/100);
            XPManager.setXP(bw.getName(), k, XPManager.getXP(bw.getName(), k)+count);
            if (count >= golbal){
                bw.broadcast("§b起床战争§f>>§e"+kname+"§f击杀§e"+pname+"§f并无情的掠夺了§b"+count+"§f经验!!");
            }
            String Message = getConfig().getString("Message");
            if (Message != null){
                k.playSound(k.getLocation(), SoundMachine.get("ORB_PICKUP", "ENTITY_EXPERIENCE_ORB_PICKUP"), 10.0F, 1.0F);
                TitleAPI.sendTitle(k,1,2,1," ","§b+"+count+"§a经验");
            }
            else{
            }
        }
    }
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (sender.hasPermission("killxp.reload")||sender.isOp()){
        this.reloadConfig();
        sender.sendMessage("§b§lKillXp重载成功!");
        }
        return false;
    }
}

